﻿
<div class="row-fluid">
    <div class="span3 sidebar-nav-fixed">
        @Html.Partial("~/Views/Partial/Search.cshtml")
    </div>
    <div class="span9">
        @Html.Partial("~/Views/Partial/FeaturedHome.cshtml")
    </div>
</div>

@section scripts{

    <script>
        //Search Form
        //$('#journeyDate').attr("data-date", new Date());
        
        $('#journeyDate').datepicker({ defaultDate: "+1d",
            changeMonth: true,
            numberOfMonths: 2,
            dateFormat: "yy-dd-mm"
        });

        $(document).ready(function () {
            $("#btnSearch").click(function () {
                if(!isValid()) return;

                var date = $("#journeyDate").val();
                var queryString = "t=" + $("#hdFromCity").val() + "~" + $("#hdToCity").val() + "~" + date;
                var url = "/bus/result/?" + queryString;
                $(location).attr('href', url);
            });
        });

        function isValid()
        {
            var flag =true;
            $("#divFromCity").removeClass("error");
            $("#divToCity").removeClass("error");
            $("#divDate").removeClass("error");

            if($("#hdFromCity").val() == ""){
                $("#divFromCity").addClass("error").fadeOut().fadeIn(400).fadeOut().fadeIn(400);
                flag=false;
             }
             else if($("#hdToCity").val() == ""){
                $("#divToCity").addClass("error").fadeOut().fadeIn(400).fadeOut().fadeIn(400);
                flag=false;
             }
             else if($("#journeyDate").val() == ""){
               $("#divDate").addClass("error").fadeOut().fadeIn(400).fadeOut().fadeIn(400);
               flag=false;
             }

            //control-group error
            return flag;
        }
        //Set Search form
        /*var input = getParameterByName("t");
        var lines = input.split('~');
        if (lines.length == 3) {
            $("#fromCity").val(lines[0]);
            $("#toCity").val(lines[1]);
            $("#journeyDate").val(lines[2]);
        }*/

        $('#fromCity').autocomplete({

            //make ajax call
            source: function (request, response) {
                $.ajax({
                    url: "Api/CityApi/",
                    type: 'GET',
                    cache: false,
                    data: request,
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    success: function (json) {
                        // call autocomplete callback method with results  
                        response($.map(json, function (data, val) {
                            return {
                                label: data.CityName + "( " + data.StateName + " )",
                                value: data.CityId
                            }
                        }));
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('Error occured while autocomplete');
                    },                    
                });
            },
            minLength: 1,
            select: function (event, ui) {
                $("#hdFromCity").val(ui.item.value);
                $(this).val(ui.item.label);
                return false;
            }
        });

        $('#toCity').autocomplete({

            //make ajax call
            source: function (request, response) {
                $.ajax({
                    url: "Api/CityApi/", //TODO: Pass from city id
                    type: 'GET',
                    cache: false,
                    data: request,
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    success: function (json) {
                        // call autocomplete callback method with results  
                        response($.map(json, function (data, val) {
                            return {
                                label: data.CityName + "( " + data.StateName + " )",
                                value: data.CityId
                            }
                        }));
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('Error occured while autocomplete');
                    },                    
                });
            },
            minLength: 1,
            select: function (event, ui) {
                $("#hdToCity").val(ui.item.value);
                $(this).val(ui.item.label);
                return false;
            }
        });
    </script>
}